package com.lrh.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lrh.reggie.common.BaseContext;
import com.lrh.reggie.entity.ShoppingCart;
import com.lrh.reggie.mapper.ShoppingCartMapper;
import com.lrh.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品和套餐 到 购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart addShoppingCart(ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart addCartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (addCartServiceOne != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = addCartServiceOne.getNumber();
            addCartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(addCartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            addCartServiceOne = shoppingCart;
        }
        return addCartServiceOne;
    }


    /**
     * 减少  购物车  的 菜品和套餐
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart subShoppingCart(ShoppingCart shoppingCart) {

        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);

        Long dishId = shoppingCart.getDishId();

        if (dishId != null) {
            // 判断减少 的是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //  判断减少  的还是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart subCartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);

        //查询当前菜品或者套餐的数量是否为 1
        Integer subCartNumber = subCartServiceOne.getNumber();

        if (subCartNumber > 1) {
            //如果大于 1 ，就在原来数量基础上减 1
            subCartServiceOne.setNumber(subCartNumber - 1);
            shoppingCartService.updateById(subCartServiceOne);
        } else {
            //如果小于等于 1 ，就将该菜品或套餐删除
            subCartServiceOne.setNumber(0); // 不做这个设置的话 回显不正常
            shoppingCartService.removeById(subCartServiceOne);
        }

        return subCartServiceOne;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        shoppingCartService.remove(queryWrapper);
    }
}
